'''
Created on Dec 4, 2012

@author: hbliu
'''

import math
terms = [[98012, 102, 1835, 51], [96322, 133, 3525, 20], [98524, 119, 1118, 34], [99824, 143, 23, 10]]
def X2Calc(item):
#    print item
    n00 = item[0]*1.0
    n10 = item[1]*1.0
    n01 = item[2]*1.0
    n11 = item[3]*1.0
    N = n00 + n10 + n01 + n11
#    print N
    X2 = ( N * ((n11*n00 - n10*n01)**2) ) / ( (n00+n01)*(n10+n11)*(n00+n10)*(n01+n11) )
    log_n11 = (n11/N) * math.log( (N*n11) / ((n11+n10)*(n11+n01)), 2 )
    log_n01 = (n01/N) * math.log( (N*n01) / ((n01+n00)*(n01+n11)), 2 )
    log_n10 = (n10/N) * math.log( (N*n10) / ((n10+n11)*(n10+n00)), 2 )
    log_n00 = (n00/N) * math.log( (N*n00) / ((n00+n01)*(n00+n10)), 2 )
    I = log_n11 + log_n01 + log_n10 + log_n00
    print X2, I

#test = [774106, 27652, 141, 49]
for item in terms:
    X2Calc(item)
